local puyun = fk.CreateSkill{
  name = "plum__puyun",
  tags = { Skill.Combo },

  dynamic_desc = function (self, player, lang)
    local condition = player:getTableMark(self.name)
    local trans = function (c)
      return c == 1 and "" or "②"
    end
    return string.format("plum__puyun_inner:%s:%s:%s", trans(condition[1]), trans(condition[2]), trans(condition[3]))
  end
}

Fk:loadTranslationTable{
  [puyun.name] = "谱韵",
  [":"..puyun.name] = "连招技（“弄笛”+“弄笛”+“弄笛”），若全因“弄笛②”达成，你对至多三名角色发动“琴音”，否则你摸两张牌并将一个连招条件改为“弄笛②”直到回合结束。",

  [":plum__puyun_inner"] = "连招技（“弄笛{1}”+“弄笛{2}”+“弄笛{3}”），若全因“弄笛②”达成，你对至多三名角色发动“琴音”，否则你摸两张牌并将一个连招条件改为“弄笛②”直到回合结束。",

  ["#plum__puyun-change"] = "谱韵：改变一个连招条件",
  ["#plum__puyun-qinyin"] = "谱韵：请选择至多三名目标，对其发动“琴音”",
  ["#plum__puyun-qinyin_choose"] = "琴音：选择令这些角色执行的效果",

  ["$plum__puyun1"] = "",
  ["$plum__puyun2"] = "",
}

puyun:addAcquireEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, puyun.name, {1, 1, 1})
end)

puyun:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, puyun.name, 0)
end)

puyun:addEffect(fk.AfterSkillEffect, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(puyun.name) and data.skill.name == "plum__nongdi" then
      local condition = player:getTableMark(puyun.name)
      local combo = player:getTableMark("nongdi_combo")
      local times = 0
      if #combo < 3 then return end
      for _, c in ipairs(combo) do
        if c >= condition[times + 1] then
          times = times + 1
        end
        if times == 3 then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local combo = player:getTableMark("nongdi_combo")
    room:setPlayerMark(player, "nongdi_combo", 0)

    if #table.filter(combo, function (c) return c == 2 end) == 3 then
      room:notifySkillInvoked(player, puyun.name, "control")
      player:broadcastSkillInvoke(puyun.name, 2)
      local tos = room:askToChoosePlayers(player, {
        targets = room:getAlivePlayers(false),
        max_num = 3,
        min_num = 1,
        skill_name = puyun.name,
        cancelable = true,
        prompt = "#plum__puyun-qinyin"
      })

      if #tos > 0 then
        local choice = room:askToChoice(player, {
          choices = {"lose_hp", "heal_hp"},
          cancelable = false,
          prompt = "#plum__puyun-qinyin_choose",
          skill_name = puyun.name,
        })
        room:sortByAction(tos)
        for _, p in ipairs(tos) do
          if choice == "lose_hp" then
            room:loseHp(p, 1, puyun.name)
          elseif p:isWounded() then
            room:recover{
              who = p,
              num = 1,
              recoverBy = player,
              skillName = puyun.name,
            }
          end
        end
      end

    else
      room:notifySkillInvoked(player, puyun.name, "drawcard")
      player:broadcastSkillInvoke(puyun.name, 1)
      player:drawCards(2, puyun.name)

      if not player:isAlive() then return end
      local condition = player:getTableMark(puyun.name)
      local choices = {}
      for i = 1, 3 do
        if condition[i] == 1 then
          table.insert(choices, tostring(i))
        end
      end
      local choice = room:askToChoice(player, {
        choices = choices,
        cancelable = false,
        prompt = "#plum__puyun-change",
        skill_name = puyun.name,
      })

      condition[tonumber(choice)] = 2
      room:setPlayerMark(player, puyun.name, condition)
    end
  end
})

puyun:addEffect(fk.TurnEnd, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(puyun.name)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, puyun.name, {1, 1, 1})
  end
})

return puyun